﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Rison.BusinessFacade.Helper
{
    public sealed class FileHelper
    {
        public static string Message = string.Empty;

        private FileHelper()
        {
        }

        /// <summary>
        /// 判断文件夹是否存在。
        /// </summary>
        /// <param name="folderPath"></param>
        /// <returns></returns>
        public static bool FolderExists(string folderPath)
        {
            return Directory.Exists(folderPath);
        }

        /// <summary>
        /// 判断文件夹是否存在。
        /// </summary>
        /// <param name="folderPath"></param>
        /// <param name="createWhenNotExists">指定如果不存在时是否进行创建。</param>
        /// <returns></returns>
        public static bool FolderExists(string folderPath, bool createWhenNotExists)
        {
            bool exists = Directory.Exists(folderPath);
            
            if (!exists)
            {
                if (createWhenNotExists)
                {
                    try
                    {
                        Directory.CreateDirectory(folderPath);
                    }
                    catch (IOException ex)
                    {
                        Message = "创建文件路径错误，可能是因为没有足够权限：" + ex.Message;
                        exists = false;
                    }
                    catch (Exception ex)
                    {
                        Message = "创建文件路径错误，可能是因为没有足够权限：" + ex.Message;
                        exists = false;
                    }
                }
            }

            return exists;
        }

        /// <summary>
        /// 判断文件是否存在。
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool FileExists(string filePath)
        {
            FileInfo info = new FileInfo(filePath);

            return info.Exists;
        }


        /// <summary>
        /// 删除文件。
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool DeleteFile(string filePath)
        {
            try
            {
                FileInfo info = new FileInfo(filePath);
                if (info.Exists)
                    info.Delete();
                else
                {
                    Message = "文件不存在！"; return false;
                }
                
                info = null;
                return true;
            }
            catch (Exception ex)
            {
                Message = "删除文件错误，请检查文件是否正在使用中：" + ex.Message;
            }

            return false;
        }

        public static bool DeleteFileWithVirtualPath(string virtualPath)
        {
            if (virtualPath.StartsWith("/"))
                return DeleteFile(System.Web.HttpContext.Current.Server.MapPath("~" + virtualPath));
            else
                return DeleteFile(System.Web.HttpContext.Current.Server.MapPath("~/" + virtualPath));
        }

        /// <summary>
        /// 拷贝文件。
        /// </summary>
        /// <param name="sourcePath"></param>
        /// <param name="destPath"></param>
        /// <returns></returns>
        public static bool CopyTo(string sourcePath, string destPath)
        {
            return CopyTo(sourcePath, destPath, false);
        }

        public static bool CopyTo(string sourcePath, string destPath, bool overwrite)
        {
            try
            {
                FileInfo sInfo = new FileInfo(sourcePath);
                if (sInfo.Exists)
                {
                    sInfo.CopyTo(destPath, overwrite);
                }
                else
                {
                    Message = "源文件不存在！"; return false;
                }
            }
            catch (Exception ex)
            {
                Message = "拷贝文件错误，可能是因为没有足够权限：" + ex.Message;
            }

            return false;
        }


        
    }
}
